Game = class('GameState'):include(Stateful)

function Game:initialize()
	self.player = Player:new()
	--love.keyboard.setKeyRepeat(true)
	self.collider_poll_distance = 512
	self.images = {}
	self.images.platform_one = love.graphics.newImage('img/rectangle128.png')
	self.images.star_box = love.graphics.newImage('img/star_box-32.png')
	self.static_objects = {}
	self:gotoState("basic_world")		

end

function Game:near_colliders()

	for i = #self.colliders, 1, -1 do

		local d = distance(self.player.x, self.player.y, self.colliders[i].x, self.colliders[i].y)
		--print(d)
		if d < self.collider_poll_distance and self.colliders[i] then
			--print("near collider found")			
			self.player:check_collision(self.colliders[i])		

		end
	end

end

function Game:update_static_objects(dt)

	for i = 1, #self.static_objects do
		self.static_objects[i]:update(dt)
	end

end


function Game:handle_player_collision(collider, direction)
	collider:on_collision(self.player, direction)
end


function Game:keypressed(key, isRepeat)
	--print(key .. " pressed")
	if key == "escape" then
		love.event.quit()
	end
	self.player:keypressed(key, isRepeat)
end

function Game:keyreleased(key)
	--print(key .. " released")
	self.player:keyreleased(key)
end

function Game:mousepressed(x, y, button)
	print("mouse " .. button .. " pressed at x: " .. x .. " y: " .. y)
	self.player:mousepressed(x, y, button)
end

function Game:mousereleased(x, y, button)
	print("mouse " .. button .. " pressed at x: " .. x .. " y: " .. y)
	self.player:mousereleased(x, y, button)
end

-- world must define self.world_gravity and self.gravity_objects
function Game:gravity(dt)
	local sg = self.gravity_objects
	
	for i = 1, #sg do		
		if not sg[i].on_ground and sg[i].br_y < self.ground_level then
			
			sg[i].y = sg[i].y + self.world_gravity * dt
			--print("world_gravity" .. self.world_gravity)
		--elseif not sg[i].on_ground then
			--sg[i].on_ground = true
			--sg[i].jumping = false
			--sg[i].jump_times = sg[i].jump_times_base			
			--sg[i].y = self.ground_level-sg[i].height
		end
	end

end

function Game:quit()

end

require'src.game_states.basic_world'
